/**
 * Copyright 2011 classic-commerce
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package ca.ioniq.server.resource.impl;

import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import ca.ioniq.server.resource.ItemResource;
import ca.ioniq.server.service.InventoryService;
import ca.ioniq.server.service.exception.InventoryException;
import ca.ioniq.shared.entity.Item;

public class ItemResourceImpl implements ItemResource {
	
    private InventoryService inventoryService;

    @Inject
    public ItemResourceImpl(InventoryService inventoryService) {
        this.inventoryService = inventoryService;
    }

	public Item put(Item item) {
		return inventoryService.addItem(item);
	}

	public void update(Item item) {
		try {
			inventoryService.updateItem(item, item.getId());
		} catch (InventoryException e) {
			throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
		}
	}

	public void delete(Long id) {
		 try {
			inventoryService.removeItem(id);
		} catch (InventoryException e) {
			throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
		}		
	}

	public List<Item> getItemsByCategoryAndCompany(Long categoryId, Long companyId) {
		return inventoryService.getItemsByCategoryAndCompany(categoryId, companyId);
	}

	public List<Item> getItemsByBrand(Long brandId) {
		return inventoryService.getItemsByBrand(brandId);
	}

}
